using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Imagination.Physics
{
    public class TetherConstraint : Constraint
    {
        private Thing thing;
        private Vector3 tetherPosition;
        private float length;
        private Force springForce;

        public TetherConstraint(Thing thing, Vector3 tetherPosition, float length)
        {
            this.thing = thing;
            this.tetherPosition = tetherPosition;
            this.length = length;
            springForce = new Force(Vector3.Zero, thing);
            springForce.IsTemporary = true;
            //thing.Forces.Add(springForce);
        }

        public override void Update(GameTime gameTime, Thing thing)
        {
            Vector3 direction = tetherPosition - thing.Position;
            float distance = direction.Length() - this.length;
            if (distance > 0)
            {
                //direction.Normalize();
                //springForce.Acceleration = Vector3.Clamp(direction * distance, Vector3.Zero, new Vector3(10f, 10f, 10f));
                springForce.Acceleration = direction;
                thing.Forces.Add(springForce);
            }
            else
            {
                springForce.Acceleration = Vector3.Zero;
            }
        }
    }
}
